/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * 递归
 * @param {TreeNode} root
 * @return {boolean}
 */
 var isSymmetric = function(root) {
    /**
     * 1. 确定递归函数参数  返回值
     * 2. 确定终止条件
     * 3. 确定递归逻辑
     */
    const tools = (left,right) => {
        //第二步
        if(left==null&&right==null) return true   
        else if(left==null&&right!==null) return false
        else if(left!==null&&right==null) return false
        else if(left.val!==right.val) return false
        //第三步
        let out = tools(left.left,right.right)
        let inside = tools(left.right,right.left)
        return out&&inside
    }
    return tools(root.left,root.right)
};

/**
 * 迭代法
 * 借助队列
 * @param {TreeNode} root
 * @return {boolean}
 */
 var isSymmetric = function(root) {
    let queue =[root.left,root.right]
    while(queue.length){
        let left = queue.shift()
        let right = queue.shift()
        if(left===null&&right!==null) return false
        else if(left!==null&&right===null) return false
        else if(left===null&&right===null) continue
        else if(left.val!==right.val) return false
        queue.push(left.left)
        queue.push(right.right)
        queue.push(left.right)
        queue.push(right.left)
    }
    return true
};
